import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///点击时具有水波纹效果的text按钮
///borderWidth>0 时 decorativeColor是边框的颜色，否则是背景装饰颜色
class RippleEffectTextButton extends StatefulWidget {
  static const Color defultSplashColor = Color(0xAACCCCCC);
  static const EdgeInsetsGeometry defaultPadding = EdgeInsets.symmetric(horizontal: 10);

  static defaultFun() {}
  final double height;
  final double width;
  final Color decorativeColor;
  final double borderRadius;
  final String text;
  final Color textColor;
  final double fontSize;
  final Function onTap;
  final double borderWidth;
  final TextDecoration textDecoration;
  final EdgeInsetsGeometry padding;
  final AlignmentGeometry alignment;
  final Color splashColor;

  RippleEffectTextButton(
      {this.height,
      this.width,
      this.borderRadius = 0,
      this.textColor,
      this.fontSize = 15,
      this.text,
      this.onTap = defaultFun,
      this.borderWidth = 0,
      this.alignment=Alignment.center,
      this.padding = defaultPadding,
      this.textDecoration = TextDecoration.none,
      this.decorativeColor = Colors.transparent,
      this.splashColor = defultSplashColor});

  @override
  State<RippleEffectTextButton> createState() => _RippleEffectState();
}

class _RippleEffectState extends State<RippleEffectTextButton> {
  @override
  Widget build(BuildContext context) {
    return Material(///增加Material层，避免外层container 有decoration时不起作用
      type: MaterialType.transparency,
      borderRadius: BorderRadius.circular(widget.borderRadius),
      child: Ink(
        height: widget.height,
        width: widget.width,
        decoration: widget.borderWidth > 0
            ? BoxDecoration(
                border: Border.all(color: widget.decorativeColor, width: widget.borderWidth),
                borderRadius: BorderRadius.circular(widget.borderRadius),
              )
            : BoxDecoration(
                color: widget.decorativeColor,
                borderRadius: BorderRadius.circular(widget.borderRadius),
              ),
        child: InkWell(
          borderRadius: BorderRadius.circular(widget.borderRadius),
          onTap: widget.onTap,
          splashColor: widget.splashColor,
          child: Container(
            padding: widget.padding,
            alignment: widget.alignment,
            child: Text(
              widget.text ?? "",
              textScaleFactor: 1,
              style: TextStyle(color: widget.textColor, fontSize: widget.fontSize, decoration: widget.textDecoration),
            ),
          ),
        ),
      ),
    );
  }
}
